/*
 * Copyright 2016-2017 lei.xu<xulei2008xulei@163.com>.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.san21go.jfinfunc.financ.norm;

import org.san21go.jfinfunc.AbstractFunction;
import org.san21go.jfinfunc.FinFuncRuntimeException;
import org.san21go.jfinfunc.jfinfunc.math.AVERAGE;
import org.san21go.jfinfunc.math.stdev.STDEVS;

/**
 * Information Ratio 信息比率<br/>
 * 衡量单位超额风险带来的超额收益。信息比率越大，说明该策略单位跟踪误差所获得的超额收益越高，因此，信息比率较大的策略的表现要优于信息比率较低的基准。
 * 合理的投资目标应该是在承担适度风险下，尽可能追求高信息比率。 Information Ratio=(Rp−Rm)/σt<br/>
 * Rp=策略年化收益率<br/>
 * Rm=基准年化收益率<br/>
 * σt=策略与基准每日收益差值的年化标准差<br/>
 * 
 * @author xulei
 *
 */
public class InformationRatio extends AbstractFunction<Double> {

	/**
	 * 年化收益率
	 */
	private double[] yieldRates;
	/**
	 * 基准收益率
	 */
	private double[] baseRates;

	public InformationRatio(double[] yieldRates, double[] baseRates) {
		super();
		this.yieldRates = yieldRates;
		this.baseRates = baseRates;
	}

	@Override
	public Double evaluate() {
		if (yieldRates == null || baseRates == null || baseRates.length == 0 || yieldRates.length == 0) {
			throw new FinFuncRuntimeException("参数错误");
		}
		if (yieldRates.length != baseRates.length) {
			throw new FinFuncRuntimeException("参数错误");
		}
		// Rp=策略年化收益率
		double Rp = new AVERAGE(yieldRates).evaluate();
		// Rm=基准年化收益率
		double Rm = new AVERAGE(baseRates).evaluate();

		double[] diff = new double[yieldRates.length];
		for (int i = 0; i < yieldRates.length; i++) {
			diff[i] = yieldRates[i] - baseRates[i];
		}
		// σt=策略与基准每日收益差值的年化标准差
		double σt = new STDEVS(diff).evaluate();
		if (Double.compare(σt, 0) == 0) {
			return 0D;
		}
		return (Rp - Rm) / σt;
	}

}
